import Vue from 'vue'
import Vuex from 'vuex'
import persistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    cart:[]
  },
  mutations: {
    //加入购物车
    addcart(state,obj){
      state.cart.push(obj)
    },
    //数量加
    addnum(state,index){
      state.cart[index].number++
    },
    //数量减
    catnum(state,index){
      state.cart[index].number--
    },
    //删除
    del(state,index){
      state.cart.splice(index,1)
    },
    //复选框
    xuan(state,index){
      state.cart[index].flag=!state.cart[index].flag
    },
    //删除选中
    xs(state){
      
      // console.log(state.cart)
      for(var i=0;i<state.cart.length;i++){
      //  console.log( state.cart[i].flag)
       if(state.cart[i].flag){
         state.cart.splice(i,1)
         i--
       }
      }
    }
  },
  getters:{
    count(state){
       var count=0
       state.cart.forEach((item,key)=>{
         if(item.flag){
          count+=item.number*item.price
         }
         
       })
       return count
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [persistedState()]
})
